package ControllerSrevlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import MYJDBC.jdbc;

/**
 * Servlet implementation class User_Reg
 */
@WebServlet("/User_Reg")
public class User_Reg extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public User_Reg() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		String UserNumber = request.getParameter("usernumber");
		String UserPasd = request.getParameter("pass2");
		String Username = request.getParameter("username");
		
		String quser = "";
		
		
		if(UserNumber == "") {
			request.setAttribute("regtext","请填写号码~" );
			request.getRequestDispatcher("BlueVideoReg").forward(request, response);
		}else {
			jdbc jc = new jdbc();
			ResultSet rs = jc.SqlRs("select * from user_index where user_number = '"+UserNumber+"'");
			try {
				while(rs.next()) {
					quser = rs.getString("user_number");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			if(UserNumber.equals(quser)) {
				request.setAttribute("regtext","该号码已经注册过了~" );
				request.getRequestDispatcher("BlueVideoReg").forward(request, response);
			}else {
				//注册成功！
				try {
					jc.SqlUpdate("insert into user_index (user_name,user_pasd,user_number,user_xb,user_hongt,user_img) value ('"+Username+"','"+UserPasd+"','"+UserNumber+"',"+0+","+1+",'user2.jpg');");
					jc.UpdateClose();
					rs = jc.SqlRs("select * from user_index where user_number = '"+UserNumber+"'");
					int userid = 0;
					while(rs.next()) {
						userid = rs.getInt("user_id");
					}
					jc.QueryClose();
					jc.SqlUpdate("insert into user_friend (user_id,user_myfid) value ("+userid+","+11+");");
					jc.SqlUpdate("insert into user_friend (user_id,user_myfid) value ("+11+","+userid+");");
					jc.UpdateClose();
					request.setAttribute("usernumberlogin", UserNumber);
					request.setAttribute("userpsadlogin", UserPasd);
					request.setAttribute("SqlUser",jc.getSqlUser());
					request.setAttribute("SqlPass",jc.getSqlPass());
					request.setAttribute("SqlName", jc.getSqlName());
					request.getRequestDispatcher("BlueVideoLogin").forward(request, response);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		

		
	}

}
